package com.augurit.gzcgwsp.ywcmn.service.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.ywcmn.dao.YwComFsxxDao;
import com.augurit.gzcgwsp.ywcmn.entity.YwComFsxx;
import com.augurit.gzcgwsp.ywcmn.service.IYwComFsxxService;

@Service
@Transactional
public class YwComFsxxServiceImpl implements IYwComFsxxService {
		
	@Autowired 
	private YwComFsxxDao ywComFsxxDao;

	/**
	 * 获取需要提前一天发送提醒的
	 * @return
	 */
	public List<YwComFsxx> getPreNoice() {
		Date date = new Date();
		String dateStr = DateUtils.dateTimeToString(date, "yyyyMMdd");
		String hql = "from YwComFsxx ps where ps.sendTime='"+dateStr+"' and ps.moblie is not null and ps.isSend is null";
		Query query = ywComFsxxDao.getSession().createQuery(hql);
		List<YwComFsxx> list = query.list();
		return list;
	}
	
	public void changeIsSend(YwComFsxx ywComFsxx){
		String hql = "update YwComFsxx ps set ps.isSend='1' where ps.id='"+ywComFsxx.getId();
		Query query = ywComFsxxDao.getSession().createQuery(hql);
		query.executeUpdate();
	}
	
	public List<YwComFsxx> getAll() {
		return ywComFsxxDao.getAll();
	}

	public YwComFsxx getYwComFsxx(Long id) {
		return ywComFsxxDao.get(id);
	}
	
	public YwComFsxx getYwComFsxxByProcInstId(String procInstId) {
		String hql = "from YwComFsxx ps where ps.procInstId=:procInstId";
		Query query = ywComFsxxDao.getSession().createQuery(hql);
		query.setString("procInstId", procInstId);
		List<YwComFsxx> list = query.list();
		if(list.size()>0){
			return list.get(0);
		}
		return null;
	}

	public void saveOrUpdateYwComFsxx(YwComFsxx ywComFsxx) {
		ywComFsxxDao.getSession().saveOrUpdate(ywComFsxx);
	}

}
